<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <script>

    var reg = /(?<name>ab)c(?<num>23)/;
      // var reg = /(ab)c(23)/;
    var text = 'abc23a';

    var isPass = reg.test(text);
    console.log('isPass ==> ', isPass);

    //exec(): 如果验证通过, 返回一个数组, 否则返回null
    var result = reg.exec(text);
    console.log('result ==> ', result);

    //compile(): 修改正则表达式, 修改原正则表达式
    var reg1 = /hello/;
    
    var str = 'hu{1,2}(ye*)?t';
    reg1.compile(str);
    console.log('reg1 ==> ', reg1);

    var t = 'huuyt';

    if (reg1.test(t)) {
      console.log('验证通过');
    } else {
      console.log('验证不通过');
    }

    //修饰符
    //i: 忽略大小写
    //m: 多行匹配
    //g: 全局匹配

    // var r2 = /A/i;
    var r2 = new RegExp('A', 'i');
    console.log('r2 ==> ', r2);
    // var r2 = /A/ig;
    // var r2 = /A/mig;

    var t2 = 'abc';
    console.log(r2.test(t2));

    var r2 = /mark$/m;
    var t2 = 'mark\n123';
    console.log('t2 ==> ', t2);
    console.log(r2.test(t2));


    //replace()

    //敏感词库
    var database = ['小趴菜', 'TMD', '卧槽', '草', '一波了']; 

    //游戏文本
    var gameText = '小趴菜,快上啊,小趴菜,赶紧一波了,快一波了,草,还不来,TMD,毁灭了,我累了';

    // gameText = gameText.replace('小趴菜', '***');
    // gameText = gameText.replace(/小趴菜/g, '***');

    database.forEach(function (value) {
      var reg = new RegExp(value, 'g');
      let char = '';
      for (var i = 0; i < value.length; i++) {
        char += '*';
      }
      gameText = gameText.replace(reg, char);
    })
    console.log('gameText ==> ', gameText);

    var content = '生的钟灵毓秀，，，仙姿佚貌';

    //match()
    // var s1 = content.match('钟灵');
    // var s1 = content.match(/钟@?灵/);
    var s1 = content.match(/(?<t>钟@?灵)毓秀/);
    console.log('s1 ==> ', s1);
    
    //search()
    // var s2 = content.search('钟灵');
    var s2 = content.search(/钟灵/);
    console.log('s2 ==> ', s2);

    //split()
    // var s3 = content.split('，');
    var s3 = content.split(/，+/);
    console.log('s3 ==> ', s3);


  </script>
</body>
</html>